package com.ctrl.riveradministrator.mvp.view.activity.bulletin;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.Bulletin;
import com.ctrl.riveradministrator.mvp.view.weight.ExpandLayout;
import com.ctrl.riveradministrator.mvp.view.weight.ImageViewActivity;
import com.ctrl.riveradministrator.mvp.view.weight.DragFloatingActionButton;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class BulletinInfoActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;

    private CardView c12, c14, c15, c16, c18, c19, c20, c21;
    private ExpandLayout e1, e2, e3, e4, e6, e7, e8, e9;
    private TextView jb, mc, xzq, bm, lxdh, bianm, sszf, lxr, jddh, hzzz, ztmb;
    private TextView sjhz, sjhzdh, sjhzfzhd, sjhzfzhdcd, sjhzfzhdqd, sjhzfzhdzd, sjhzzz;
    private TextView shjhz, shjhzdh, shjhzfzhd, shjhzfzhdcd, shjhzfzhdqd, shjhzfzhdzd, shjhzzz;
    private TextView sjjz, sjjzdh, sjjzfzhd, sjjzfzhdcd, sjjzfzhdqd, sjjzfzhdzd, sjjzzz;
    private TextView xjhz, xjhzdh, xjhzfzhd, xjhzfzhdcd, xjhzfzhdqd, xjhzfzhdzd, xjhzzz;
    private TextView xzhz, xzhzdh, xzhzfzhd, xzhzfzhdcd, xzhzfzhdqd, xzhzfzhdzd, xzhzzz;
    private TextView xzjz, xzjzdh, xzjzfzhd, xzjzfzhdcd, xzjzfzhdqd, xzjzfzhdzd, xzjzzz;
    private TextView cjhz, cjhzdh, cjhzfzhd, cjhzfzhdcd, cjhzfzhdqd, cjhzfzhdzd, cjhzzz;
    private TextView remark, loc;
    private DragFloatingActionButton plane;
    private SimpleDraweeView imageView;
    private GenericDraweeHierarchyBuilder builder;
    private GenericDraweeHierarchy hierarchy;
    private ImageRequest request;
    private DraweeController controller;
    private Uri uri;

    private Bulletin bulletin;

    @Override
    protected void initParams(Bundle parms) {
        bulletin = (Bulletin) getIntent().getSerializableExtra("bulletin");
        uri = Uri.parse(Constants.BASE_URL_ + bulletin.getPath());
        builder = new GenericDraweeHierarchyBuilder(getResources());
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_bulletin_info;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        leftMenu = (ImageView) $(R.id.left_iv);
        remark = (TextView) $(R.id.remark);
        loc = (TextView) $(R.id.loc);
        imageView = (SimpleDraweeView) $(R.id.b_im);
        plane = (DragFloatingActionButton) $(R.id.upload_dfab);
        initCardView();
        initTitleView();
        initSjhzView();
        initShjhzView();
        initXjhzView();
        initSjjzView();
        initXzhzView();
        initXzjzView();
        initCjhzView();
        initExpandView();
        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(bulletin.getPsignName());
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        jb.setText(bulletin.getGradeName());
        mc.setText(bulletin.getPsignName());
        xzq.setText(bulletin.getPsignAreaName());
        bm.setText(bulletin.getDepart());
        lxdh.setText(bulletin.getTelephone());
        bianm.setText(bulletin.getCode());
        sszf.setText(bulletin.getGov());
        lxr.setText(bulletin.getContacts());
        jddh.setText(bulletin.getSuperviseTele());
        hzzz.setText(bulletin.getProRiverJob());
        ztmb.setText(bulletin.getTarget());

        sjhz.setText(bulletin.getProRiverAdminName());
        sjhzdh.setText(bulletin.getProRiverTele());
        sjhzfzhd.setText(bulletin.getProRiverName());
        sjhzfzhdcd.setText(bulletin.getProWarnLength());
        sjhzfzhdqd.setText(bulletin.getProRiverBegin());
        sjhzfzhdzd.setText(bulletin.getProWarnEnd());
        sjhzzz.setText(bulletin.getProRiverJob());

        shjhz.setText(bulletin.getShiRiverAdminName());
        shjhzdh.setText(bulletin.getShiRiverTele());
        shjhzfzhd.setText(bulletin.getShiRiverName());
        shjhzfzhdcd.setText(bulletin.getShiWarnLength());
        shjhzfzhdqd.setText(bulletin.getShiRiverBegin());
        shjhzfzhdzd.setText(bulletin.getShiWarnEnd());
        shjhzzz.setText(bulletin.getShiRiverJob());

        sjjz.setText(bulletin.getWarnAdminName());
        sjjzdh.setText(bulletin.getWarnTele());
        sjjzfzhd.setText(bulletin.getWarnName());
        sjjzfzhdcd.setText(bulletin.getWarnLength());
        sjjzfzhdqd.setText(bulletin.getWarnBegin());
        sjjzfzhdzd.setText(bulletin.getWarnEnd());
        sjjzzz.setText(bulletin.getWarnJob());

        xjhz.setText(bulletin.getRiverAdminName());
        xjhzdh.setText(bulletin.getRiverTele());
        xjhzfzhd.setText(bulletin.getRiverName());
        xjhzfzhdcd.setText(bulletin.getRiverLength());
        xjhzfzhdqd.setText(bulletin.getRiverBegin());
        xjhzfzhdzd.setText(bulletin.getRiverEnd());
        xjhzzz.setText(bulletin.getRiverJob());

        xzhz.setText(bulletin.getTownRiverAdminName());
        xzhzdh.setText(bulletin.getTownRiverTele());
        xzhzfzhd.setText(bulletin.getTownRiverName());
        xzhzfzhdcd.setText(bulletin.getTownRiverLength());
        xzhzfzhdqd.setText(bulletin.getTownRiverBegin());
        xzhzfzhdzd.setText(bulletin.getTownRiverEnd());
        xzhzzz.setText(bulletin.getTownRiverJob());

        xzjz.setText(bulletin.getTownWarnAdminName());
        xzjzdh.setText(bulletin.getTownWarnTele());
        xzjzfzhd.setText(bulletin.getTownWarnName());
        xzjzfzhdcd.setText(bulletin.getTownWarnLength());
        xzjzfzhdqd.setText(bulletin.getTownWarnBegin());
        xzjzfzhdzd.setText(bulletin.getTownWarnEnd());
        xzjzzz.setText(bulletin.getTownWarnJob());

        cjhz.setText(bulletin.getVilRiverAdminName());
        cjhzdh.setText(bulletin.getVilRiverTele());
        cjhzfzhd.setText(bulletin.getVilRiverName());
        cjhzfzhdcd.setText(bulletin.getVilRiverLength());
        cjhzfzhdqd.setText(bulletin.getVilRiverBegin());
        cjhzfzhdzd.setText(bulletin.getVilRiverEnd());
        cjhzzz.setText(bulletin.getVilRiverJob());

        switch (bulletin.getGradeId()) {
            case "1":
                c12.setVisibility(View.VISIBLE);
                e1.setVisibility(View.VISIBLE);
                break;
            case "2":
                c14.setVisibility(View.VISIBLE);
                e2.setVisibility(View.VISIBLE);
                c15.setVisibility(View.VISIBLE);
                e3.setVisibility(View.VISIBLE);
                break;
            case "3":
                c16.setVisibility(View.VISIBLE);
                e4.setVisibility(View.VISIBLE);
                break;
            case "4":
                c20.setVisibility(View.VISIBLE);
                e8.setVisibility(View.VISIBLE);
                break;
        }


        hierarchy = imageView.getHierarchy();
        hierarchy.setFailureImage(R.mipmap.ic_launcher);
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        //imageView.setImageURI(uri);
        if (bulletin.getPath() == null || bulletin.getPath().equals("")) {
            return;
        }
        request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(100, 100))
                .build();
        controller = Fresco.newDraweeControllerBuilder()
                .setOldController(imageView.getController())
                .setImageRequest(request)
                .build();
        imageView.setController(controller);
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.c12:
                e1.toggleExpand();
                break;
            case R.id.c14:
                e2.toggleExpand();
                break;
            case R.id.c15:
                e3.toggleExpand();
                break;
            case R.id.c16:
                e4.toggleExpand();
                break;
            case R.id.c18:
                e6.toggleExpand();
                break;
            case R.id.c19:
                e7.toggleExpand();
                break;
            case R.id.c20:
                e8.toggleExpand();
                break;
            case R.id.c21:
                e9.toggleExpand();
                break;
            case R.id.b_im:
                Intent intent = new Intent(this, ImageViewActivity.class);
                intent.putExtra("img_url", Constants.BASE_URL_ + bulletin.getPath());
                intent.putExtra("type", "net");
                startActivity(intent);
                break;
            case R.id.left_iv:
                finish();
                break;
            case R.id.upload_dfab:
                Intent intentP = new Intent(this, FullScreenActivity.class).putExtra("bulletin", bulletin);;
                startActivity(intentP);
                break;
        }
    }

    @Override
    protected void setListener() {
        c12.setOnClickListener(this);
        c14.setOnClickListener(this);
        c15.setOnClickListener(this);
        c16.setOnClickListener(this);
        c18.setOnClickListener(this);
        c19.setOnClickListener(this);
        c20.setOnClickListener(this);
        c21.setOnClickListener(this);
        leftMenu.setOnClickListener(this);
        imageView.setOnClickListener(this);
        plane.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void initCardView() {
        c12 = (CardView) $(R.id.c12);
        c14 = (CardView) $(R.id.c14);
        c15 = (CardView) $(R.id.c15);
        c16 = (CardView) $(R.id.c16);
        c18 = (CardView) $(R.id.c18);
        c19 = (CardView) $(R.id.c19);
        c20 = (CardView) $(R.id.c20);
        c21 = (CardView) $(R.id.c21);
    }

    private void initTitleView() {
        jb = (TextView) $(R.id.jb);
        mc = (TextView) $(R.id.mc);
        xzq = (TextView) $(R.id.xzq);
        bm = (TextView) $(R.id.bm);
        lxdh = (TextView) $(R.id.lxdh);
        bianm = (TextView) $(R.id.bianm);
        sszf = (TextView) $(R.id.sszf);
        lxr = (TextView) $(R.id.lxr);
        jddh = (TextView) $(R.id.jddh);
        hzzz = (TextView) $(R.id.hzzz);
        ztmb = (TextView) $(R.id.ztmb);
    }

    private void initSjhzView() {
        sjhz = (TextView) $(R.id.sjhz);
        sjhzdh = (TextView) $(R.id.sjhzdh);
        sjhzfzhd = (TextView) $(R.id.sjhzfzhd);
        sjhzfzhdcd = (TextView) $(R.id.sjhzfzhdcd);
        sjhzfzhdqd = (TextView) $(R.id.sjhzfzhdqd);
        sjhzfzhdzd = (TextView) $(R.id.sjhzfzhdzd);
        sjhzzz = (TextView) $(R.id.sjhzzz);
    }

    private void initShjhzView() {
        shjhz = (TextView) $(R.id.shjhz);
        shjhzdh = (TextView) $(R.id.shjhzdh);
        shjhzfzhd = (TextView) $(R.id.shjhzfzhd);
        shjhzfzhdcd = (TextView) $(R.id.shjhzfzhdcd);
        shjhzfzhdqd = (TextView) $(R.id.shjhzfzhdqd);
        shjhzfzhdzd = (TextView) $(R.id.shjhzfzhdzd);
        shjhzzz = (TextView) $(R.id.shjhzzz);
    }

    private void initSjjzView() {
        sjjz = (TextView) $(R.id.sjjz);
        sjjzdh = (TextView) $(R.id.sjjzdh);
        sjjzfzhd = (TextView) $(R.id.sjjzfzhd);
        sjjzfzhdcd = (TextView) $(R.id.sjjzhdcd);
        sjjzfzhdqd = (TextView) $(R.id.sjjzhdqd);
        sjjzfzhdzd = (TextView) $(R.id.sjjzfzhd);
        sjjzzz = (TextView) $(R.id.sjjzzz);
    }

    private void initXjhzView() {
        xjhz = (TextView) $(R.id.xjhz);
        xjhzdh = (TextView) $(R.id.xjhzdh);
        xjhzfzhd = (TextView) $(R.id.xjhzfzhd);
        xjhzfzhdcd = (TextView) $(R.id.xjhzhdcd);
        xjhzfzhdqd = (TextView) $(R.id.xjhzqd);
        xjhzfzhdzd = (TextView) $(R.id.xjhzhdzd);
        xjhzzz = (TextView) $(R.id.xjhzhzzz);
    }


    private void initXzhzView() {
        xzhz = (TextView) $(R.id.xzhz);
        xzhzdh = (TextView) $(R.id.xzhzdh);
        xzhzfzhd = (TextView) $(R.id.xzhzfzhd);
        xzhzfzhdcd = (TextView) $(R.id.xzhzfzhdcd);
        xzhzfzhdqd = (TextView) $(R.id.xzhzfzhdqd);
        xzhzfzhdzd = (TextView) $(R.id.xzhzfzhdzd);
        xzhzzz = (TextView) $(R.id.xzhzzz);
    }

    private void initXzjzView() {
        xzjz = (TextView) $(R.id.xzjz);
        xzjzdh = (TextView) $(R.id.xzjzdh);
        xzjzfzhd = (TextView) $(R.id.xzjzfzhd);
        xzjzfzhdcd = (TextView) $(R.id.xzjzfzhdcd);
        xzjzfzhdqd = (TextView) $(R.id.xzjzfzhdqd);
        xzjzfzhdzd = (TextView) $(R.id.xzjzfzhdzd);
        xzjzzz = (TextView) $(R.id.xzjzzz);
    }

    private void initCjhzView() {
        cjhz = (TextView) $(R.id.cjhz);
        cjhzdh = (TextView) $(R.id.cjhzdh);
        cjhzfzhd = (TextView) $(R.id.cjhzfzhd);
        cjhzfzhdcd = (TextView) $(R.id.cjhzfzhdcd);
        cjhzfzhdqd = (TextView) $(R.id.cjhzfzhdqd);
        cjhzfzhdzd = (TextView) $(R.id.cjhzfzhdzd);
        cjhzzz = (TextView) $(R.id.cjhzzz);
    }

    private void initExpandView() {
        e1 = (ExpandLayout) $(R.id.e1);
        e2 = (ExpandLayout) $(R.id.e2);
        e3 = (ExpandLayout) $(R.id.e3);
        e4 = (ExpandLayout) $(R.id.e4);
        e6 = (ExpandLayout) $(R.id.e6);
        e7 = (ExpandLayout) $(R.id.e7);
        e8 = (ExpandLayout) $(R.id.e8);
        e9 = (ExpandLayout) $(R.id.e9);
        e1.initExpand(false);
        e2.initExpand(false);
        e3.initExpand(false);
        e4.initExpand(false);
        e6.initExpand(false);
        e7.initExpand(false);
        e8.initExpand(false);
        e9.initExpand(false);
    }
}
